<?php 

namespace Admin\Controller;


class CategoryController extends BaseController{
    public function index(){
       
        // 获取所有栏目
        $cats = M('category')->select();
        
        $cats_str = '';
        foreach ($cats as $v){
            $cats_str .= '{ id:'.$v['id'].',pId:'.$v['pid'].',name:"'.$v['cat_name'].'",path:"'.U('categoryAdd',array('cat_id'=>$v['id'])).'"},';
        }
        
        $this->assign('cats_str',$cats_str);
        
        $this->display('categoryList');
    }
    
    public function categoryAdd(){
        if (IS_POST){
            
            // 创建模型对象
            $cat_M = D('Category');
            
//             dump(I('post.'));die;
            // 自动验证
            $data = $cat_M->create();
            if (!$data){
                $this->ajaxReturn(array('status'=>0,'msg'=>$cat_M->getError()));die;
            }
            
            if(I('post.cat_id')>0){
                unset($data['cat_id']);
                $res = $cat_M->where(array('id'=>I('post.cat_id')))->save($data);
                if ($res == 0){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'未修改数据'));
                }
            }else{
                // 添加数据
                $res = $cat_M->add($data);
            }
            
            if ($res !== false){
                $this->ajaxReturn(array('status'=>1));
            }else {
                $this->ajaxReturn(array('status'=>0,'msg'=>'添加失败，请重试！'));
            }
        }elseif(I('get.cat_id') > 0){
            $cat_id = I('get.cat_id');
            $category = M('category');
            // 获取栏目信息
            $cat = $category->find($cat_id);
            if (I('add')==1){
                
                // 获取上一级分类名称
                $p_name = $cat['cat_name'];
                
            }else {
               
                // 获取上级分类名称
                if ($cat['pid'] > 0){
                    $p_name = $category->where(array('id'=>$cat['pid']))->getField('cat_name');
                }else {
                    $p_name = '第一级分类';
                }
                $this->assign('cat',$cat);
            }
            
            // 传递数据
            $this->assign('p_name',$p_name);
            $this->assign('cat_id',$cat_id);
            
            $this->display();
        }else {
            
            $this->display();
        }
        
    }
    
    // 删除分类
    public function categoryDel(){
        $cat_id = I('get.cat_id');
        
        // 删除当前分类
        $res = M('category')->delete($cat_id);
        
        if (1){
            $data = M('category')->field('id,pid')->select();
            
            // 获取子分类
            $res = getChild($data,$cat_id);
            
            // 删除子分类
            foreach ($res as $v){
                M('category')->delete($v['id']);
            }
            
            $this->ajaxReturn(array('status'=>1));
        }else {
            $this->ajaxReturn(array('status'=>0));
        }
    }
    
}
?>